/**
 * Created by Administrator on 2016/3/17 0017.
 */
'use strict'

import Vue from 'vue'
import Router from 'vue-router'

import IndexView from './page/IndexView.vue'
import RecommendView from './page/RecommendView.vue'
import PreferView from './page/PreferView.vue'
import HomeView from './page/HomeView.vue'
import UserInfoView from './page/UserInfoView.vue'
import LuckyView from './page/LuckyView.vue'
import WalletView from './page/WalletView.vue'
import TaskView from './page/TaskView.vue'
import GetMoneyView from './page/GetMoneyView.vue'
import App from './page/App.vue'
import {setSession,isLogin,getSession} from './lib/session'
import server from './lib/server'
import {weixinUrl} from './lib/args'
import FastClick from 'fastclick'
import {getTicket} from './lib/weixinShare'

FastClick.attach(document.body);

var winH=window.document.documentElement.clientHeight,
    winW=window.document.documentElement.clientWidth;
document.getElementsByTagName('html')[0].style.fontSize=`${16/340*(winH<winW?winH:winW)}px`;

Vue.use(Router);

var router=new Router();

router.map({
    '/':{
        name:'index',
        component:IndexView
    },
    'home':{
        name:'home',
        component:HomeView
    },
    'user':{
        name:'user',
        component:UserInfoView
    },
    'recommend':{
        name:'recommend',
        component:RecommendView
    },
    'prefer':{
        name:'prefer',
        component:PreferView
    },
    'lucky':{
        name:'lucky',
        component:LuckyView
    },
    'wallet':{
        name:'wallet',
        component:WalletView
    },
    'task':{
        name:'task',
        component:TaskView
    },
    'money':{
        name:'money',
        component:GetMoneyView
    }
});

router.redirect({
    '*': '/'
});

router.beforeEach(({ to, next })=>{
    if(to.name=='index'){
        next();
    }else{
        var session=getSession();
        if(isLogin()){
            if(to.name=='recommend'&&session.attr){
                router.go('home');
            }else{
                next();
            }
        }else{
            var unionid=localStorage['unionid']||to.query.unionid;
            if(unionid){
                server('/P/Weixin/checkuser',{
                    unionid:unionid
                }).success((respose)=>{
                    if(respose.code==1){
                        localStorage['unionid']=unionid;
                        session=respose.data;
                        setSession(session);
                        if(to.name=='recommend'&&session.attr){
                            router.go('home');
                        }else{
                            next();
                        }
                    }else{
                        localStorage.removeItem('unionid');
                        router.go('/');
                    }
                });
            }else{
                window.location.href=weixinUrl;
            }
        }
    }
});
router.afterEach((to)=>{
    getTicket();
});
router.start(App, '#app');